import java.io.IOException;
import java.util.UUID;

import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;


public class UsuarioFactory {

	private static byte [] TABELA_USUARIO = Bytes.toBytes("usuario");
	private static byte [] ATRIBUTOS_USUARIO = Bytes.toBytes("atributos");
	private static byte [] LOGIN = Bytes.toBytes("login");
	private static byte [] SENHA = Bytes.toBytes("senha");

	private static byte [] TABELA_LOGIN = Bytes.toBytes("login");
	private static byte [] ATRIBUTOS_LOGIN = Bytes.toBytes("atributos");
	private static byte [] ID = Bytes.toBytes("id");

	private void criarLogin(String login, String id) throws IOException {
		HTable htable = new HTable(TABELA_LOGIN);
		Put put = new Put(Bytes.toBytes(login)); 
		put.add(ATRIBUTOS_LOGIN, ID, Bytes.toBytes(id));
		htable.put(put);
	}
	
	private String criarUsuario(String login, String senha) throws IOException {
		HTable htable = new HTable(TABELA_USUARIO);
		String id = criarUUID();
		Put put = new Put(Bytes.toBytes(id)); 
		put.add(ATRIBUTOS_USUARIO, LOGIN, Bytes.toBytes(login));
		put.add(ATRIBUTOS_USUARIO, SENHA, Bytes.toBytes(senha));
		htable.put(put);
		return id;
	}
	
	public boolean criar(String login, String senha) throws IOException {
		
		if (loginJahUsado(login) ) {
			return false;
		}
		
		String id = criarUsuario(login, senha);
		criarLogin(login, id);
		return true;
	}
	
	private boolean loginJahUsado(String login2) {
		
		return false;
	}

	public String criarUUID() {
		return UUID.randomUUID().toString();
	}
	
	public static void main(String[] args) throws IOException {
		if ( args.length != 2 ) {
			System.err.println("Quantidade de parametros incorreta, deveriam ser dois parametros!");
			System.exit(1);
		}
		
		new UsuarioFactory().criar(args[0], args[1]);
	} 
	
	
}
